package com.example.demo01minio;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import io.swagger.v3.core.util.Json;
import okio.Utf8;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.junit.jupiter.api.Test;

import java.io.IOException;

/**
 * ClassName:HttpClientUtil
 * Package:com.example.demo01minio
 * Description:
 *
 * @Author:helei
 * @Create:2024/9/4 - 16:45
 */
public class HttpClientUtil {

    //java发送网络请求
    //常见框架：HttpClient，springmvc-RestTemplate ，okHttp
    //分布式架构：服务之间远程访问 上面的三个框架都可以实现 RestTemplate
    @Test
    void test1() throws IOException {
        //1、创建客户端对象
        DefaultHttpClient client = new DefaultHttpClient();
        //2、创建请求对象
        // 封装请求地址+请求方式
//        HttpGet httpGet = new HttpGet("http://localhost:8080/admin/system/user/page");
        HttpGet httpGet = new HttpGet("http://www.atguigu.com");
        //3、封装请求参数 或者 请求体 :参考阿里云短信的HttpUtils工具类

        //4、发送请求 得到响应报文对象
        CloseableHttpResponse response = client.execute(httpGet);
        //解析响应报文
        int statusCode = response.getStatusLine().getStatusCode();
        if (statusCode == 200) {
            //解析成功的响应体：
            String json = EntityUtils.toString(response.getEntity(), "UTF-8");
//            JSONObject jsonObject = JSON.parseObject(json, JSONObject.class);

            System.out.println(json);
//            System.out.println(jsonObject);
        }
        //6、关闭客户端对象
        client.close();

    }
}
